package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentStoreCommodityBinding
import com.vitabuybuy.tv.dto.GoodsDto
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.ui.shopcenter.GoodsDetailsActivity
import com.vitabuybuy.tv.ui.shopcenter.SearchGoodsOrStoreActivity
import com.vitabuybuy.tv.ui.shopcenter.viewmodel.SearchGoodsOrStoreViewModel
import kotlinx.android.synthetic.main.fragment_store_commodity.etSearch
import kotlinx.android.synthetic.main.fragment_store_commodity.mTabLayout_1
import kotlinx.android.synthetic.main.fragment_store_commodity.refresh
import java.util.ArrayList

class StoreCommodityFragment :
    BaseFragment<SearchGoodsOrStoreViewModel, FragmentStoreCommodityBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_store_commodity
    private var pageNumber = 1
    private var sort = "featured"
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private var storeId: Int? = -1


    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel

        mTabEntities.add(TabEntity(getString(R.string.recommend), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.new_product), 0, 0))
        mTabEntities.add(
            TabEntity(
                getString(R.string.price),
                R.drawable.icon_common_updown,
                R.drawable.icon_common_updown
            )
        )
        mTabLayout_1.setTabData(mTabEntities)
        storeId = (activity as StoreHomeActivity).getStoreId()
    }

    override fun lazyLoadData() {
        viewModel.getGoodsList(
            "",
            storeId,
            null,
            sort,
            pageNumber
        )
    }

    override fun eventHandling() {
        super.eventHandling()
        etSearch.setOnClickListener {
            SearchGoodsOrStoreActivity.openActivity(mContext, storeId)
        }
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                viewModel.getGoodsList(
                    "",
                    storeId,
                    null,
                    sort,
                    pageNumber
                )
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                viewModel.getGoodsList(
                    "",
                    storeId,
                    null,
                    sort,
                    pageNumber
                )
            }
        })
        mTabLayout_1.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                when (position) {
                    0 -> {
                        sort = "featured"
                        mTabEntities[2] =
                            TabEntity(
                                getString(R.string.price),
                                R.drawable.icon_common_updown,
                                R.drawable.icon_common_updown
                            )
                    }
                    1 -> {
                        sort = "new"
                        mTabEntities[2] =
                            TabEntity(
                                getString(R.string.price),
                                R.drawable.icon_common_updown,
                                R.drawable.icon_common_updown
                            )
                    }
                    2 -> {//点击了价格,价格从高到低
                        sort = "down_price"
                        mTabEntities[2] = TabEntity(
                            getString(R.string.price),
                            R.drawable.icon_common_updown_down,
                            R.drawable.icon_common_updown
                        )
                    }
                }
                viewModel.getGoodsList(
                    "",
                    storeId,
                    null,
                    sort,
                    pageNumber
                )
                mTabLayout_1.setTabData(mTabEntities)
            }

            override fun onTabReselect(position: Int) {
                if (position == 2) {//一直点击价格
                    if (sort == "up_price") {//价格从高到低
                        sort = "down_price"
                        mTabEntities[2] =
                            TabEntity(
                                getString(R.string.price),
                                R.drawable.icon_common_updown_down,
                                R.drawable.icon_common_updown
                            )
                    } else if (sort == "down_price") {//从低到高是
                        sort = "up_price"
                        mTabEntities[2] =
                            TabEntity(
                                getString(R.string.price),
                                R.drawable.icon_common_updown_up,
                                R.drawable.icon_common_updown
                            )
                    }
                    pageNumber = 1
                    viewModel.getGoodsList(
                        "",
                        storeId,
                        null,
                        sort,
                        pageNumber
                    )
                }
                mTabLayout_1.setTabData(mTabEntities)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                GoodsDetailsActivity.openActivity(mContext, (msg.obj as GoodsDto).id)
            }
        }
    }

}
